package com.sansung;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.border.Border;

import org.jdesktop.swingx.JXBusyLabel;
import org.jvnet.substance.SubstanceLookAndFeel;

/**
 * 登录窗体
 * 
 * @author songchao
 * 
 */
public class Login extends JFrame {

	/**
	 * UUID
	 */
	private static final long serialVersionUID = -4540652657983569463L;
	
	private static JFrame loginFrame;
	private JPanel jpanel;
	private JTextField tfName;// 登录名称
	private JPasswordField tfPass;// 密码
	private JButton buttonLogin;
	private ImageIcon ii;
	private JLabel lab, lab1,labExit,labMin;
	private JCheckBox savePassword;
	final ClassLoader classloader;
	private JXBusyLabel jblabel = new JXBusyLabel(new Dimension(35, 35));
	final Point p1=new Point();//鼠标点击点
	final Point p2=new Point();//拖拽到达点
	
	public Login() {
//		loginFrame=this;
		jpanel = new JPanel();// 用来放图片
		jpanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				p1.x=e.getPoint().x;
				p1.y=e.getPoint().y;
				super.mousePressed(e);
			}
			
		});
		jpanel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				p2.x=e.getPoint().x;
				p2.y=e.getPoint().y;
				Point p=getLocation();//新的位置点
				p.x+=p2.x-p1.x;
				p.y+=p2.y-p1.y;
				setLocation(p);
				super.mouseDragged(e);
			}
		});
		classloader = Thread.currentThread().getContextClassLoader();
		Font font = new Font("宋体", Font.PLAIN, 12);// 创建字体以便其它调用
		Border border = BorderFactory.createEmptyBorder();
		ii = new ImageIcon(classloader.getResource("com/sansung/resources/images/login2.png"));
		
		jblabel.setText("正在登陆.....");
		jblabel.setFont(font);
		jblabel.setVisible(false);
		jblabel.setBusy(true);
		jblabel.setIcon(new ImageIcon(getClass().getResource("resources/images/blue-loading.gif")));
		this.setSize(340, 230);
		setUndecorated(true);
		setAlwaysOnTop(true);
		tfName = new JTextField(20);
		tfName.setBorder(border);
		tfName.setBounds(130, 100, 180, 23);
		tfName.setOpaque(false);// 设置为透明

		tfPass = new JPasswordField(20);
		tfPass.setBorder(border);
		tfPass.setOpaque(false);// 设置为透明
		tfPass.setBounds(130, 135, 180, 23);

		lab1 = new JLabel("<html>找回密码</html>");
		lab1.setCursor(new Cursor(Cursor.HAND_CURSOR));// 鼠标变成手型
		lab1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				new ForgetPassword();
//				ForgetPassword.startApplication();
			}
		});
		lab1.setFont(font);
		lab1.setForeground(Color.blue);
		lab1.setBounds(210, 160, 53, 23);
		lab1.setOpaque(false);
		lab1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				lab1.setText("<html><u>找回密码</u></html>");
				super.mouseEntered(e);
			}
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				lab1.setText("<html>找回密码</html>");
				super.mouseExited(e);
			}
		});
			
		savePassword=new JCheckBox();
		savePassword.setOpaque(false);
		savePassword.setBounds(103, 164, 20, 20);
		
		File file = new File("C:\\unicloud\\pwinfo.dat");
		if (!file.exists()) {
		} else {
//			Map<String, String> mapusername = fileoperateservice
//					.getApplicationInfoAboutPassword();
//			if (mapusername == null) {
//			} else {
//				tfName.setText(mapusername.get("username"));
//				tfPass.setText(mapusername.get("password"));
//				tfIP.setText(mapusername.get("ip"));
//			}
		}

		buttonLogin = new JButton(
				new ImageIcon(
						classloader.getResource("com/sansung/resources/images/loginbutton.png")));
		buttonLogin.setFocusPainted(false);// 隐藏组件上文字的边框
		buttonLogin.setBorder(border);
		buttonLogin.setFont(font);
		buttonLogin.setCursor(new Cursor(Cursor.HAND_CURSOR));
		buttonLogin.setBounds(233, 203, 73, 23);
		buttonLogin.setOpaque(false);
		buttonLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loginStart();
			}
		});
		
		labExit = new JLabel();
		labExit.setCursor(new Cursor(Cursor.HAND_CURSOR));// 鼠标变成手型
		labExit.setBounds(309, 0, 30, 23);
		labExit.setOpaque(false);
		labExit.setToolTipText("关闭");
		labExit.setIcon(new ImageIcon(getClass().getResource("resources/images/exit.png")));
		labExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if(e.getButton()==1){
					System.exit(0); 
				}
				super.mouseClicked(e);
			}
		});
		
		labMin = new JLabel();
		labMin.setCursor(new Cursor(Cursor.HAND_CURSOR));// 鼠标变成手型
		labMin.setBounds(279, 0, 30, 23);
		labMin.setOpaque(false);
		labMin.setToolTipText("最小化");
		labMin.setIcon(new ImageIcon(getClass().getResource("resources/images/min.png")));
		labMin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if(e.getButton()==1){
					loginFrame.setExtendedState(JFrame.ICONIFIED);
				}
				super.mouseClicked(e);
			}
		});
		
		jpanel.setBounds(7, 96, 340, 170);
		jpanel.setLayout(null);// 布局为空
		jpanel.setOpaque(false);
		
		jpanel.add(tfName);
		jpanel.add(tfPass);
		jpanel.add(lab1);
		jpanel.add(savePassword);
		
		jblabel.setBounds(80, 190, 200, 40);
		jpanel.add(buttonLogin);
		jpanel.add(jblabel);
		jpanel.add(labExit);
		jpanel.add(labMin);
		// 获取回车事件
		tfName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					loginStart();
				}
			}
		});
		tfPass.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					loginStart();
				}
			}
		});
		buttonLogin.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					loginStart();
				}
			}
		});

		//pan.add(back);// 把放有组件的容器放装图片的容器上
		jpanel.setLayout(null);
		jpanel.setOpaque(false);
		lab = new JLabel(ii);// 把图片放在JLAbel上
		lab.setBounds(0, 0, ii.getIconWidth(), ii.getIconHeight());// 大小由图片的大小决定
		this.getLayeredPane().setLayout(null);
		this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
		this.setContentPane(jpanel);
		this.setTitle("SANSUNG管理系统V1.0");
		this.setIconImage(new ImageIcon(classloader
				.getResource("com/sansung/resources/images/logo.png"))
				.getImage());
		this.setResizable(false);// 设置尺寸不可改变
		this.setLocationRelativeTo(null);// 设置居中
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 当点击关闭时进行关闭
		this.setVisible(true);
	}

	private void loginStart() {
		try {
			UIManager
					.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel");
			SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(true);
			if (tfName.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "登录名不能为空！");
				return;
			} else if (String.valueOf(tfPass.getPassword()).equals("")) {
				JOptionPane.showMessageDialog(this, "密码不能为空！");
				return;
			} else {
				jblabel.setVisible(true);
				new SwingWorker<Boolean, String>() {
					@Override
					protected Boolean doInBackground() throws Exception {
						String loginName = tfName.getText();
						String password = String.valueOf(tfPass.getPassword());
						Thread.sleep(1000);
//						publish("chunks");setProgress(1);
						return null;
					}
	
					@Override
					protected void process(List<String> chunks) {
						// TODO Auto-generated method stub
						
					}

					@Override
					protected void done() {
						try {
							get();
							jblabel.setVisible(false);
							new MainFrame().start();
							loginFrame.dispose();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}.execute();
	
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		loginFrame=new Login();
	}
}
